<?php
declare (strict_types = 1);
namespace app\index\model\user;

use think\Model;

/**
 * @mixin \think\Model
 */
class WechatUser extends Model
{
    protected $pk = "id";

    /**
     * 获取信息
     * @param $openId
     * @return array|null|Model
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    function getInfoByOpenId($openId){
        return $this->where('openid',$openId)->find();
    }

    /**
     * 获取信息
     * @param $id
     * @return array|null|Model
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    function getInfoById($id,$field = '*'){
        $where = ['wechat_user_id'=>$id];
        return $this->where($where)->field($field)->find();
    }

    /**
     * 添加微信用户记录
     * @param $addData
     * @return int|string
     */
    function addWeChatUser($addData,$openId,$userType){
        $addData = [
            'user_id'=>0,
            'openid'=>$openId,
            'nickname'=>isset($addData['nickname']) ? $addData['nickname'] : '',
            'headimgurl'=>isset($addData['avatar']) ? $addData['avatar'] : '',
            'sex'=>isset($addData['original']['sex']) ? $addData['original']['sex'] : 0,
            'city'=>isset($addData['original']['city']) ? $addData['original']['city'] : '',
            'language'=>isset($addData['original']['language']) ? $addData['original']['language'] : '',
            'province'=>isset($addData['original']['province']) ? $addData['original']['province'] : '',
            'country'=>isset($addData['original']['country']) ? $addData['original']['country'] : '',
            'user_type'=>$userType,
            'create_time'=>date("Y-m-d H:i:s"),
            'update_time'=>date("Y-m-d H:i:s"),
        ];
        return $this->insertGetId($addData);
    }

    /**
     * 修改微信用户记录
     * @param $wechatUerId
     * @param $saveData
     * @return static
     */
    function updateWeChatUser($wechatUerId,$saveData){
        return $this->where('wechat_user_id',$wechatUerId)->update($saveData);
    }

    /**
     * 获取信息
     * @param $where
     * @return array|null|Model
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    function getInfoByWhere($where){
        return $this->where($where)->find();
    }

    /**
     * 添加微信小程序用户记录
     * @param $addData
     * @return int|string
     */
    function addWeChatAppletUser($addData,$openId,$userType){

        $addData = [
            'user_id'=>0,
            'routine_openid'=>$openId,
            'nickname'=>isset($addData['nickName']) ? $addData['nickName'] : '',
            'headimgurl'=>isset($addData['avatarUrl']) ? $addData['avatarUrl'] : '',
            //'sex'=>isset($addData['original']['sex']) ? $addData['original']['sex'] : 0,
            'city'=>isset($addData['city']) ? $addData['city'] : '',
            'language'=>isset($addData['language']) ? $addData['language'] : '',
            'province'=>isset($addData['province']) ? $addData['province'] : '',
            'country'=>isset($addData['country']) ? $addData['country'] : '',
            'user_type'=>$userType,
            'create_time'=>date("Y-m-d H:i:s"),
            'update_time'=>date("Y-m-d H:i:s"),
        ];
        return $this->insertGetId($addData);
    }

    /**
     * 小程序绑定手机号
     * @param $wechatUser
     * @param $addData
     * @param $userType
     * @return mixed
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    function appletBindUser($wechatUser,$addData,$userType){
        $userModel = new User();
        $userInfo = $userModel->getUserByAccount($addData['phone']);
        if(!$userInfo){
            $userData = [
                'phone'=>$addData['phone'],
                'user_type'=>$userType,
                'avatar'=>$wechatUser['headimgurl'],
                'nickname'=>$wechatUser['nickname'],
            ];
            $addUserId = $userModel->registerUser($userData);
            if(!$addUserId) return $wechatUser;
            $userInfo = $userData;
            $userInfo['id'] = $addUserId;
        }
        //绑定用户记录id
        $saveData = ['user_id'=>$userInfo['id'],'update_time'=>date("Y-m-d H:i:s")];
        $result = $this->where('wechat_user_id',$wechatUser['wechat_user_id'])->update($saveData);
        if($result){
            $wechatUser['user_id'] = $userInfo['id'];
        }
        return $wechatUser;
    }

}
